package com.comone.webrtcaudio;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.comone.webrtcaudio.wav.AudioPlayer;
import com.comone.webrtcaudio.wav.WavFileReader;

import java.io.IOException;
import java.nio.ByteBuffer;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.startView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                play();
            }
        });
        findViewById(R.id.startView2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stop=true;
                voicePlayer.play();
            }
        });
    }

    private AudioPlayer audioPlayer;
    private boolean stop = false;
    private VoicePlayer voicePlayer;

    private void play() {
        WebRTCAudioUtils webRTCAudioUtils = new WebRTCAudioUtils();
        long aecmInst = webRTCAudioUtils.aecmCreate();
        int aecmInit = webRTCAudioUtils.aecmInit(aecmInst, 8000);
        int aecmConfig = webRTCAudioUtils.aecmSetConfig(aecmInst, webRTCAudioUtils.getAecmConfig((short)1, (short)3));

        long nsxInst = webRTCAudioUtils.nsxCreate();
        int nsxInit = webRTCAudioUtils.nsxInit(nsxInst, 8000);
        int nexSetPolicy = webRTCAudioUtils.nsxSetPolicy(nsxInst, 2);

        voicePlayer= new VoicePlayer();
        voicePlayer.start(8000);

        new Thread(() -> {
            WavFileReader wavFileReader = new WavFileReader();
            try {
                wavFileReader.openFile("/sdcard/aaaaa/output8.wav");
            } catch (IOException e) {
                e.printStackTrace();
            }
            audioPlayer = new AudioPlayer();
            audioPlayer.startPlayer(0);

            byte[] buffer = new byte[320];
            while (!stop) {
                if (wavFileReader.readData(buffer, 0, buffer.length) > 0) {
//                    audioPlayer.play(byteArray2ShortArray(shortArray), 0, shortArray.length * 2);
                }
                short[] shortArray = byteArray2ShortArray(buffer);

                short[] resultFrame = new short[160];
                webRTCAudioUtils.aecmBufferFarend(aecmInst, shortArray,160);
                webRTCAudioUtils.aecmProcess(aecmInst, shortArray, null, resultFrame, 160, 0);

                double sum = 0;
                int i;
                try {
                    for (i = 0; i < resultFrame.length; i++) {
                        sum = sum + resultFrame[i];
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                Log.e("" , "resultFrame" + sum);

                short[] outNsData = new short[160];

                webRTCAudioUtils.nsxProcess(nsxInst, resultFrame, 1, outNsData);

                voicePlayer.write(outNsData);

                {
                    double out_sum = 0;
                    int j;
                    try {
                        for (j = 0; j < outNsData.length; j++) {
                            out_sum = out_sum + outNsData[j];
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    Log.e("" , out_sum + "outNsData" + ( out_sum - sum));
                }
            }
            audioPlayer.stopPlayer();
            try {
                wavFileReader.closeFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }).start();
    }

    public static byte[] byteArray2ShortArray(short[] data) {
        byte[] bytes2 = new byte[data.length * 2];
        ByteBuffer.wrap(bytes2).asShortBuffer().put(data);
        return bytes2;
    }

    public static short[] byteArray2ShortArray(byte[] data) {
        short[] shorts = new short[data.length/2];
        ByteBuffer.wrap(data).asShortBuffer().get(shorts);
        return shorts;
    }

}